package com.example.exp_20212294;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.exp_20212294.empty.UserInfo;
import com.example.exp_20212294.util.UserDbHelper;

public class RegisterActivity extends AppCompatActivity {

    private EditText et_username;
    private EditText et_password;
    private SMSReceiver smsReceiver;
    private EditText et_yanzhen;
    private EditText et_pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        et_pd = findViewById(R.id.et_pd);

        smsReceiver = new SMSReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(smsReceiver,intentFilter);
        et_yanzhen = findViewById(R.id.et_yanzhen);

        //返回
        findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //不要跳转到登录界面，直接将当前页面销毁就好了
                finish();
            }
        });

        //点击注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                String phone = et_pd.getText().toString();//因为之前设置的String，这里也可以用int

                if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(password)){
                    Toast.makeText(RegisterActivity.this,  "请输入用户名和密码",Toast.LENGTH_SHORT).show();
                }else{
                    int row = UserDbHelper.getInstance(RegisterActivity.this).register(username,password,phone);
                    if (row>0) {
                        Toast.makeText(RegisterActivity.this,"注册成功，请登录", Toast.LENGTH_SHORT).show();
                        finish();//销毁页面，同startActivity（）有一定区别，但在这里更方便
                    }

                }
            }
        });
    }

    class SMSReceiver extends BroadcastReceiver
    {

        public void onReceive(Context context, Intent intent) {
            Uri uri = Uri.parse("content://sms/");
            ContentResolver contentResolver = getContentResolver();
            Cursor cursor = contentResolver.query(uri,new String[]{"address","body"},null,null,null);
            if (cursor.moveToNext())
            {
                String address = cursor.getString(0);
                String body = cursor.getString(1);
                if (address.equals("1000"))
                {
                    int p = body.indexOf("验证码：")+"验证码：".length();
                    String code = body.substring(p,p+6);
                    et_yanzhen.setText(code);
                }
            }
            cursor.close();
        }

    }

}